package com.e.dianshang.controller.Api;

import com.e.dianshang.service.UserOrderService;
import com.e.dianshang.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@Api(value = "用户订单模块",tags = "订单api")
public class UserOrderApi {

    @Resource
    UserOrderService userOrderService;

    @Resource
    RedisTemplate redisTemplate;

    JsonResult jsonResult;

    @RequestMapping("selectMyPay")
    @ApiOperation(value = "用户已完成的订单",httpMethod = "GET")
    @ApiImplicitParam(name = "openid",value = "用户电话或者用户id",dataType = "string")
    public Map select(@RequestParam("openid") String openid){
        jsonResult=new JsonResult();
        List<Map> maps = userOrderService.selectFinishPay(openid);
        List<String> list=new ArrayList<>();
        for (Map map:maps){
            list.add(map.get("p_unique").toString());
        }
        List<Map> pay=new ArrayList<>();
        for (int i=0;i<list.size();i++){
            String unique = list.get(i);
            Map map1 = userOrderService.selectFinishPay1(unique);
            List<Map> maps1 = userOrderService.selectWaitPurchased(unique);
            List<Integer> list1=new ArrayList<>();
            for (Map map:maps1){
                list1.add(Integer.valueOf(map.get("goods_id").toString()));
            }
            for (int j =0;j<list1.size();j++){
                Integer id = list1.get(j);
                Map map = userOrderService.selectGoods(id);
                maps1.get(j).put("good"+j,map);
            }
            map1.put("goodList",maps1);
            pay.add(map1);
        }
        jsonResult.setCode("1");
        jsonResult.setData(pay);
        return jsonResult.getValues();
    }

    @RequestMapping("selectWaitPay")
    @ApiOperation(value = "用户待发货的订单",httpMethod = "GET")
    @ApiImplicitParam(name = "openid",value = "用户电话或者用户id",dataType = "string")
    public Map selectWait(@RequestParam("openid") String openid){
        jsonResult=new JsonResult();
        List<Map> maps = userOrderService.selectWaitPay(openid);
        List<String> list=new ArrayList<>();
        for (Map map:maps){
            list.add(map.get("p_unique").toString());
        }
        List<Map> waitPay=new ArrayList<>();
        for (int i=0;i<list.size();i++){
            String unique = list.get(i);
            Map map = userOrderService.selectWaitPay1(unique);
            List<Map> maps1 = userOrderService.selectWaitPurchased(unique);
            List<Integer> list1=new ArrayList<>();
            for (Map map1:maps1){
                list1.add(Integer.valueOf(map1.get("goods_id").toString()));
            }
            for (int j=0;j<list1.size();j++){
                Integer id = list1.get(j);
                Map map1 = userOrderService.selectGoods(id);
                maps1.get(j).put("goods"+j,map1);
            }
            map.put("goodList",maps1);
            waitPay.add(map);
        }
        jsonResult.setCode("1");
        jsonResult.setData(waitPay);
        return jsonResult.getValues();
    }

    @RequestMapping("updatePState")
    @ApiOperation(value = "修改订单状态",httpMethod = "POST")
    @ApiImplicitParams(@ApiImplicitParam(name = "unique,state",value = "订单编号和订单状态",dataType = "string"))
    public Map updatePState(@RequestBody Map map){
        jsonResult=new JsonResult();
        Integer integer = userOrderService.updatePState(map);
        if (integer>0){
            jsonResult.setCode("1");
            jsonResult.setMsg("修改成功");
        }else {
            jsonResult.setCode("0");
        }
        return jsonResult.getValues();
    }

    @RequestMapping("selectArrive")
    @ApiOperation(value = "用户待收货的订单",httpMethod = "GET")
    @ApiImplicitParam(name = "openid",value = "用户电话或者用户id",dataType = "string")
    public Map selectArrive(@RequestParam("openid") String openid){
        jsonResult=new JsonResult();
        List<Map> maps = userOrderService.selectArrive(openid);
        List<String> list=new ArrayList<>();
        for (Map map:maps){
            list.add(map.get("p_unique").toString());
        }
        List<Map> arrive=new ArrayList<>();
        for (int i=0;i<list.size();i++){
            String unique = list.get(i);
            Map map = userOrderService.selectArrive1(unique);
            List<Map> maps1 = userOrderService.selectWaitPurchased(unique);
            List<Integer> list1=new ArrayList<>();
            for (Map map1:maps1){
                list1.add(Integer.valueOf(map1.get("goods_id").toString()));
            }
            for (int j=0;j<list1.size();j++){
                Integer id = list1.get(j);
                Map map1 = userOrderService.selectGoods(id);
                maps1.get(j).put("goods"+j,map1);
            }
            map.put("goodList",maps1);
            arrive.add(map);
        }
        jsonResult.setCode("1");
        jsonResult.setData(arrive);
        return jsonResult.getValues();
    }

    @RequestMapping("selectWaitePayMoney")
    @ApiOperation(value = "用户待付款的订单",httpMethod = "GET")
    @ApiImplicitParam(name = "openid",value = "用户电话或者用户id",dataType = "string")
    public Map selectWaitePayMoney(@RequestParam("openid") String openid){
        jsonResult=new JsonResult();
        List<Map> maps = userOrderService.selectWaitPayMoney(openid);
        List<String> list=new ArrayList<>();
        for (Map map:maps){
            list.add(map.get("p_unique").toString());
        }
        List<Map> maps1=new ArrayList<>();
        ValueOperations valueOperations = redisTemplate.opsForValue();
        for (int i=0;i<list.size();i++){
            if (list.get(i).equals(valueOperations.get(openid+":"+list.get(i)))){
                String unique = list.get(i);
                Map map = userOrderService.selectWaitPayMoney1(unique);
                List<Map> maps2 = userOrderService.selectWaitPurchased(unique);
                List<Integer> list1=new ArrayList<>();
                for (Map map1:maps2){
                    list1.add(Integer.valueOf(map1.get("goods_id").toString()));
                }
                for (int j=0;j<list1.size();j++){
                    Integer id = list1.get(j);
                    Map map1 = userOrderService.selectGoods(id);
                    maps2.get(j).put("goods"+j,map1);
                }
                map.put("goodList",maps2);
                maps1.add(map);
            }else {
                Map map=new HashMap();
                map.put("unique",list.get(i));
                map.put("state",4);
                userOrderService.updatePState(map);
            }
        }
        if (maps1.size()>0){
            jsonResult.setData(maps1);
        }else{
            jsonResult.setMsg("你还没有相关的订单信息");
        }
        return jsonResult.getValues();
    }
}